package com.google.coffeeshop.gui.desktop.employee;

import java.util.ArrayList;
import java.util.List;

import javax.swing.table.DefaultTableModel;

import vn.bang.utility.DateUtil;
import vn.bang.utility.StringUtil;

import com.google.coffeeshop.entity.EmployeeDTO;
import com.google.coffeeshop.gui.desktop.AbstractBaseTable;

public class EmployeeTable extends AbstractBaseTable {
	private static final long serialVersionUID = 1L;
	
	private static String COL_EMP_ID = "Employee ID";
	private static String COL_IMAGE_FILENAME = "Image file name";
	private static String COL_CODE = "Code";
	private static String COL_FULL_NAME = "Full Name";
	private static String COL_BORN_DATE = "Born Date";
	private static String COL_BEGIN_DATE = "Begin Date";
	private static String COL_END_DATE = "End Date";
	
	
	private static Object[][] rowData = new Object[][] {};
	private static String[] columnNames = new String[] {
		COL_EMP_ID, COL_IMAGE_FILENAME, COL_CODE, COL_FULL_NAME, COL_BORN_DATE, COL_BEGIN_DATE, COL_END_DATE
	};
	
	private DefaultTableModel model = new DefaultTableModel(
			rowData,
			columnNames
		);
	
	public EmployeeTable() {
		super(rowData, columnNames);
		setModel(model);
		invisibleColumn(0);
		invisibleColumn(0);
	}
	
	private void invisibleColumn(int columnIndex) {
		this.getColumnModel().removeColumn(this.getColumnModel().getColumn(columnIndex));
	}
	
	private void addNewRow(long employeeId, String imageFileName, String code, String fullName, String bornDate, String beginDate, String endDate) {
		Object[] objects = new Object[] {employeeId, imageFileName, code, fullName, bornDate, beginDate, endDate};
		model.addRow(objects);
	}

	private void addNewRow(EmployeeDTO employee) {
		addNewRow(
				employee.getId(),
				employee.getImageFilename(),
				employee.getCode(), 
				employee.getFullName(), 
				DateUtil.toString(employee.getBornDate()), 
				DateUtil.toString(employee.getBeginDate()), 
				employee.getEndDate() != null ? DateUtil.toString(employee.getEndDate()) : ""
		);
	}
	
	public void addNewRows(List<EmployeeDTO> employees) {
		for (EmployeeDTO employee : employees) {
			addNewRow(employee);
		}
	}
	
	public EmployeeDTO getEmployeeSelected() {
		int rowIndex = this.getSelectedRow();
		EmployeeDTO employee = new EmployeeDTO();
		employee.setId(
				(long)findModelValue(model, COL_EMP_ID, rowIndex));
		employee.setImageFilename(
				(String)findModelValue(model, COL_IMAGE_FILENAME, rowIndex));
		employee.setCode(
				(String)findModelValue(model, COL_CODE, rowIndex));
		employee.setFullName(
				(String)findModelValue(model, COL_FULL_NAME, rowIndex));
		employee.setBornDate(
				DateUtil.toDate(
						(String)findModelValue(model, COL_BORN_DATE, rowIndex)));
		employee.setBeginDate(
				DateUtil.toDate(
						(String)findModelValue(model, COL_BEGIN_DATE, rowIndex)));
		employee.setEndDate(
				StringUtil.isNullOrEmpty((String)findModelValue(model, COL_END_DATE, rowIndex)) ? null :
					DateUtil.toDate((String)findModelValue(model, COL_END_DATE, rowIndex)));
		return employee;
	}
	
	public List<EmployeeDTO> getEmployeeList() {
		List<EmployeeDTO> employees = new ArrayList<EmployeeDTO>(); 
		for (int rowIndex = 0; rowIndex < model.getRowCount(); rowIndex++) {
			EmployeeDTO employee = getEmployeeSelected();
			employees.add(employee);
		}
		return null;
	}
	
}
